package cn.zhouyang.redistest.push;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.util.Scanner;

/**
 * @Author:ZhouYang
 * @Description:
 * @Date: Created in 2021/3/23 8:51
 * @Modified By： Redis消息订阅模式发布者
 *               键盘输入事件，发布给myChannel频道
 */
public class PushAndSub  extends Thread{

    private final JedisPool jedisPool;

    public PushAndSub(JedisPool jedisPool) {
        this.jedisPool = jedisPool;
    }

    @Override
    public void run() {
        //线程执行逻辑
        System.out.println("请输入您要发送的消息：");
        Scanner scanner = new Scanner(System.in);
        String next = scanner.next();
        System.out.println("输入数值："+next );
        //获取连接池中连接
        Jedis jedis = jedisPool.getResource();
        while (true) {
            if(!"quit".equals(next)){
                jedis.publish("myChannel", scanner.next());   //从 mychannel 的频道上推送消息
            }else{
                break;
            }
        }
    }
}
